//
//  JBMainViewController.swift
//  sinaBlog
//
//  Created by jobo on 15/10/27.
//  Copyright © 2015年 jobo. All rights reserved.
//

import UIKit

class JBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //添加首页
        let homeVc = JBHomeTableViewController()
        addTableViewController(homeVc, title: "首页", imgNomal: "tabbar_home", imgSel: "tabbar_home_highlighted")
        
         //添加消息
        let messageVc = JBMessageTableViewController()
        addTableViewController(messageVc, title: "消息", imgNomal: "tabbar_message_center", imgSel: "tabbar_message_center_highlighted")

        //添加加号
        let composeVc = UIViewController()
        addTableViewController(composeVc, title: "", imgNomal: " ", imgSel: " ")
        
        
        //添加发现
        let discoverVc = JBDiscoverTableViewController()
        addTableViewController(discoverVc, title: "发现", imgNomal: "tabbar_discover", imgSel: "tabbar_discover_highlighted")

        
        //添加我
        let profileVc = JBProfileTableViewController()
        addTableViewController(profileVc, title: "我", imgNomal: "tabbar_profile", imgSel: "tabbar_profile_highlighted")

        
        
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)
        //宽
        let width = tabBar.frame.size.width / 5
        composeBtn.frame = CGRectMake(width * 2, 0, width, tabBar.frame.size.height)
        tabBar.addSubview(composeBtn)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK -- 添加View的基本方法
    func addTableViewController (viewConrol : UIViewController , title : String , imgNomal : String , imgSel : String)
    {
        viewConrol.title = title
        viewConrol.tabBarItem.image = UIImage(named: imgNomal)
        //设置选中时候的title颜色
        viewConrol.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        //设置选中时候的image图片
        let selectImag = UIImage(named: imgSel)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        viewConrol.tabBarItem.selectedImage = selectImag
        addChildViewController(UINavigationController(rootViewController: viewConrol))
    }
    
    lazy var composeBtn : UIButton =
    {
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        btn.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn 
    
    }()
    
    func composeClick()
    {
        
        print("compose")
        presentViewController(UINavigationController(rootViewController:JBComposeViewController()), animated: true, completion: nil)
    }
    
}
